package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *访客计划
 *
 *@author weijianxun
 */
@Service("communicatePlanDynamicStrategyImpl")
public class CommunicatePlanDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {
    private final Logger LOG = LoggerFactory.getLogger(CommunicatePlanDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(CommunicatePlanEnum.LINK_CUSTOMER.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            PaasFormDataEntity newPaasFormDataEntity = saasFormSaveDTO.getNewPaasFormDataEntity();
            JSONObject newData = newPaasFormDataEntity.getData();
            String name = newData.getString(CommunicatePlanEnum.NAME.getAttr());
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, null,
                    DynamicRefInfoDTO.initDynamicRefInfoDTO(dynamicSaveDTO.getDataId(), saasFormSaveDTO.getBusinessType(), name), ProjectLogEnum.CUSTOMER_COMMUNICATE_PLAN_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.CommunicatePlanDynamicStrategyImpl.save", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            super.batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, null, CommunicatePlanEnum.NAME.getAttr(), ProjectLogEnum.CUSTOMER_COMMUNICATE_PLAN_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.CommunicatePlanDynamicStrategyImpl.batchDelete", e);
        }
    }
}
